<template>
<el-dialog title="资料修改" :visible.sync="dialogFormVisible" class="publish-box">
  <el-form ref="form" :model="form" :rules="rules"  label-width="80px">
    <el-form-item label="头像" prop="avatar">
      <el-upload
        :headers="uploadHeaders"
        class="avatar-uploader"
        action="/api/media/upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
          <img v-if="form.avatar" :src="form.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
    </el-form-item>
    <el-form-item label="相机型号" prop="camera">
      <el-input :maxlength="50"  v-model="form.camera"  />
    </el-form-item>
    <el-form-item label="个人介绍" prop="intro">
      <el-input :maxlength="240" :rows="4" v-model="form.intro" type="textarea" />
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleSubmit">确 定</el-button>
  </div>
</el-dialog>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  name: 'AccountSet',
  data() {
    return {
      dialogFormVisible: false,
      inputVisible: false,
      inputValue: '',
      rules: {
        camera: [{
          type: 'string',
          required: true,
          message: '请输入相机型号',
          trigger: 'blur',
        }],
        intro: [{
          type: 'string',
          required: true,
          message: '请填写个人介绍',
          trigger: 'blur',
        }],
        avatar: [{
          type: 'string',
          required: true,
          message: '请上传头像',
          trigger: 'blur',
        }],
      },
      form: {},
    };
  },
  computed: {
    uploadHeaders() {
      return {
        'x-csrf-token': this.$store.state.glob.csrf,
      };
    },
    ...mapState({
      originalForm: state => state.user.info,
    }),
  },
  methods: {
    ...mapActions([
      'getUserInfo',
    ]),
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const { intro, camera, avatar } = this.form;
          this.$http.post('/user/userinfo', {
            intro, camera, avatar,
          })
            .then(() => {
              this.$notify({
                title: '成功',
                message: '用户信息修改成功',
                type: 'success',
              });
              this.getUserInfo();
              this.dialogFormVisible = false;
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    show() {
      this.dialogFormVisible = true;
    },

    handleAvatarSuccess(res) {
      this.form.avatar = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = ['image/jpeg', 'image/png', 'image/gif'].includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 png/jpg/gif 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
  },
  watch: {
    dialogFormVisible(bool) {
      if (bool) {
        this.form = { ...this.originalForm };
      }
    },
  },
};
</script>

<style lang="scss">
.publish-box {
  .el-upload--picture-card,
  .el-upload-list__item {
    width: 80px;
    height: 80px;
    line-height: 88px;
  }
  .el-tag+.el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
}
</style>

<style>
  .no-border{
    border: none !important;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 80px;
    width: 80px;
    line-height: 80px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    text-align: center;
    display: inline-block;
    margin-top: calc(50% - 14px);
  }
  .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
</style>
